

// 引入字体图标
require('../../libs/fonts/iconfont.css')


// 当前样式引入
require('./information.less')

//引入weui
require('weui')
const weui=require('weui.js')

//引入axios
const axios=require('../../utlis/axios')

//引入dom.js
const {$,g,ga}=require('../../utlis/dom')

$(function(){
    g('#backBtn').addEventListener('click',function(){
        // console.log(1);
        history.back()
    })

    /**
     * 选择图片
     */
    g('#click-').addEventListener('click',function(){
        // console.log(1);
        g('#file').click()
    })

    /**
     * 上传图片
     */
    g('#file').addEventListener('change',function(){
        // console.log(1);
        // console.log(this.files[0]);
        const fd=new FormData()
        fd.append('file',this.files[0])

        axios.post('/api/shared/uploadPortrait1',fd).then(function(res){
            // console.log(res);
            if(res.data.errno === 0){
                // console.log(res.data.data.url);
                g('.renewhead').src='http://47.96.154.185:3701'+res.data.data.url
            }
        })
    })

    /**
     * 性别
     */
     g('#gender').addEventListener('click', function () {
        weui.picker([{
            label: '女',
            value: 0
        }, {
            label: '男',
            value: 1
        }], {
            onConfirm: function (result) {
                // console.log(result);
                g('#gender').textContent=result[0].label;
            },
            title: '性别'
        });
    });

    /**
     * 生日
     */
     g('#birthday').addEventListener('click', function () {
        weui.datePicker({
            start: 1980, //开始年份
            end: new Date().getFullYear(), //结束年份
            onConfirm: function (result) {
                // console.log(result);
                g('#birthday').textContent=result.map(function(v){
                    return v.value<10?'0'+v.value:v.value;
                }).join('-')
                
            },
            title: '多列选择器'
        });
    });

    /**
     * 省份
     */
    let proid=0;
     g('#province').addEventListener('click', function () {

        axios.get('api/shared/province').then(function(res){
            let arr = res.data.data.map(function(v){
                return {
                    label:v.name,
                    value:v.id
                }
            })
            // console.log(arr);
            weui.picker(arr, {
                onConfirm: function (result) {
                    // console.log(result);
                    g('#province').textContent=result[0].label;
                    g('#city').textContent='城市'
                    proid=result[0].value
                    // console.log(proid);
                },
                title: '省份'
            });
        })


    });

    /**
     * 城市
     */
    let cityid=0
    
     g('#city').addEventListener('click', function () {
        axios.get('/api/shared/city',{
            params:{
                provinceId:proid
            }
        }).then(function(res){
            if(res.data.errno === 0){
                // console.log(res);
                let arr = res.data.data.map(function(v){
                    return {
                        label:v.name,
                        value:v.id
                    }
                })
                // console.log(arr);
                weui.picker(arr, {
                    onConfirm: function (result) {
                        // console.log(result);
                        g('#city').textContent=result[0].label;
                        cityid=result[0].value; 
                    },
                    title: '城市'
                });
            }
        })   
    });
    
    // console.log(cityid);
    
    /**
     * 个性签名
     */
    g('#desc').addEventListener('input',function(){
        g('#desclen').textContent=this.value.length;
    })

    /**
     * 上传信息
     */
    g('#savebtn').addEventListener('click',function(){

        const information={
            nickName: nickname.value,
            gender: g('#gender').textContent === '男' ? 1 : 0,
            provinceId: proid,
            cityId: cityid,
            birthday: g('#birthday').textContent,
            imgUrl: g('.renewhead').src,
            sign: g('#desc').value

        }
        // console.log(1);
        axios.post('/api/user/changeInfo',information).then(function(res){
            if(res.data.errno === 0){
                localStorage.setItem('token',res.data.data.token)
                location.href='./mine.html'
            }
        })
    })
   
    /**
     * 数据回填
     */

    axios.get('/api/user/info').then(function(res){
        if (res.data.errno === 0){

            let data=res.data.data;

            g('.renewhead').src=data.imgUrl;
            g('#nickname').value=data.nickName;
            g('#gender').textContent=data.gender?'男':'女';
            g('#birthday').textContent=data.birthday;
            g('#desc').value=data.sign;
            g('#desclen').textContent = g('#desc').value.length
            
            axios.get('api/shared/province').then(function(res){
                let obj = res.data.data.filter(function(v){
                    return v.id===data.provinceId
                })
                // console.log(obj);
                g('#province').textContent=obj[0].name
                
            })
            axios.get('/api/shared/city',{
                params: {
                    provinceId: data.provinceId
                }
            }).then(function(res){
                let obj = res.data.data.filter(function(v){
                    return v.id===data.cityId
                })
                g('#city').textContent=obj[0].name
            })

        }
    })


})